//
//  CommentCell.m
//  CloudWeibo
//
//  Created by CLOUD on 1/3/15.
//  Copyright (c) 2015 Cloud. All rights reserved.
//

#import "CommentCell.h"
#import "RTLabel.h"
#import "CommentModel.h"
#import "UIImageView+WebCache.h"
#import "UIUtils.h"

@implementation CommentCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    _userImage = [(UIImageView *)[self viewWithTag:100] retain];
    _nickLabel = [(UILabel *)[self viewWithTag:101] retain];
    _timeLabel = [(UILabel *)[self viewWithTag:102] retain];
    
    _commentLabel = [[RTLabel alloc] initWithFrame:CGRectZero];
    _commentLabel.font = [UIFont systemFontOfSize:14.0f];
    _commentLabel.delegate = self;
    
    _commentLabel.linkAttributes = [NSDictionary dictionaryWithObject:@"#4595CB" forKey:@"color"];
    _commentLabel.selectedLinkAttributes = [NSDictionary dictionaryWithObject:@"darkGray" forKey:@"color"];
    [self.contentView addSubview:_commentLabel];
}


-(void) layoutSubviews
{
    [super layoutSubviews];
    
    NSString *urlString = self.commentModel.user.profile_image_url;
    [_userImage setImageWithURL:[NSURL URLWithString:urlString]];
    
    _nickLabel.text = self.commentModel.user.screen_name;
    _timeLabel.text = [UIUtils fomateString:self.commentModel.created_at];
    
    _commentLabel.frame = CGRectMake(_userImage.right+10, _nickLabel.bottom + 5, 240, 21);
    
    NSString *commentText = self.commentModel.text;
    commentText = [UIUtils parseLink:commentText];
    _commentLabel.text = commentText;
    _commentLabel.height = _commentLabel.optimumSize.height;
}


+(float) getCommentHeight:(CommentModel *)commentModel
{
    RTLabel *rtLabel = [[RTLabel alloc] initWithFrame:CGRectMake(0, 0, 240, 0)];
    rtLabel.font = [UIFont systemFontOfSize:14.0f];
    rtLabel.text = commentModel.text;
    return rtLabel.optimumSize.height;
}

- (void)rtLabel:(id)rtLabel didSelectLinkWithURL:(NSURL*)url
{
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
